public class ListNode {

    // 两个链表的第一个公共结点
    //链接：https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?tpId=13&tqId=11189&ru=/exam/oj
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
    private int len (ListNode head) {
        int count = 0;
        while(head != null) {
            head = head.next;
            count++;
        }
        return count;
    }
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        //先计算两个链表的长度
        int lenA = len(pHead1);
        int lenB = len(pHead2);

        //还有相同的情况 得特殊对待一下
        while(lenA != lenB) {
            if(lenA > lenB) {
                //第一个链表先走
                pHead1 = pHead1.next;
                lenA--;
            }else{
                pHead2 = pHead2.next;
                lenB--;
            }
        }

        //现在 长度一样 且在同一起点启动
        while(pHead1 != pHead2) {
            pHead1 = pHead1.next;
            pHead2 = pHead2.next;
        }
        return pHead1;
    }

    public static void main(String[] args) {

    }
}
